/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("Ls_pro_bom",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Entity.DomainModels;
using MyErp.IServices;
using VOL.Core.Utilities;
using System.Linq;
using MyErp.Repositories;
using VOL.Core.DBManager;
using Microsoft.EntityFrameworkCore;
using VOL.Core.Extensions;
using VOL.Core.ManageUser;

namespace MyErp.Controllers
{
    public partial class Ls_pro_bomController
    {
        private readonly ILs_pro_bomService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;

        [ActivatorUtilitiesConstructor]
        public Ls_pro_bomController(
            ILs_pro_bomService service,
            IHttpContextAccessor httpContextAccessor
        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
        }

        private WebResponseContent webResponse = new WebResponseContent();
        [HttpPost, Route("generate_npqr")]//,AllowAnonymous
        public WebResponseContent generate_npqr(System.Guid Ls_project_id)
        {
            string now_d = DateTime.Now.ToString("yyMMdd").ToString();
            string yy = now_d.ToString().Substring(0, 2);
            string mm = now_d.ToString().Substring(2, 2);
            string dd = now_d.ToString().Substring(4, 2);
            //用到了Convert.ToDateTime，先转换为list表后再，where中使用Convert.ToDateTime，否则报错
            int co = 0;
            try//报错后继续执行，因为数据库中可能没有数据，所以报错
            {
                co = DBServerProvider.DbContext.Set<Ls_pro_npqr_head>().ToList().Where(w => Convert.ToDateTime(w.CreateDate).ToString("yyMMdd") == now_d).Select(s => s.npqr_no.Substring(12).ToInt()).Max();//从当日的最大值开始
            }
            catch (Exception)
            {
                co = 0;
            }
            co = co + 1;
            string npqr_no = "NPQR-" + yy + mm + dd + "-" + co;

            //1、插入npqr主表；
            Ls_project _Project= Ls_projectRepository.Instance.FindAsIQueryable(t => (t.ls_project_id == Ls_project_id)).ToList().FirstOrDefault();
            Ls_pro_npqr_head ls_Pro_Npqr_Head= new Ls_pro_npqr_head();
            Guid guid= Guid.NewGuid();
            ls_Pro_Npqr_Head.ls_pro_npqr_head_id = guid;
            ls_Pro_Npqr_Head.npqr_no = npqr_no;
            ls_Pro_Npqr_Head.pjname = _Project.pjname;
            ls_Pro_Npqr_Head.pj_info = _Project.pj_info;
            ls_Pro_Npqr_Head.ls_project_id= _Project.ls_project_id;
            ls_Pro_Npqr_Head.customer= _Project.customer;
            ls_Pro_Npqr_Head.CreateDate = DateTime.Now;
            ls_Pro_Npqr_Head.Creator = UserContext.Current.GetUserInfo(UserContext.Current.UserId).UserTrueName;
            DBServerProvider.DbContext.Set<Ls_pro_npqr_head>().Add(ls_Pro_Npqr_Head);
            DBServerProvider.DbContext.SaveChanges();

            //2、插入npqr子表
            List<Ls_pdb> _pdb = new List<Ls_pdb>();
            _pdb = Ls_pdbRepository.Instance.FindAsIQueryable(w => (1 == 1)).ToList();

            List<Ls_pro_bom> _bom = Ls_pro_bomRepository.Instance.FindAsIQueryable(t => (t.ls_project_id == _Project.ls_project_id)).ToList();
            var update_pdb = _pdb.Where(w => _bom.Any(a => a.mara_for_ls == w.matnr)).ToList();//查询存在的物料主数据
            for (int i = 0; i < _bom.Count; i++)
            {
                Ls_pro_npqr ls_Pro_Npqr = new Ls_pro_npqr();
                ls_Pro_Npqr.ls_pro_npqr_head_id = guid;
                ls_Pro_Npqr.mara_for_cust = _bom[i].mara_for_cust;
                ls_Pro_Npqr.mara_for_sup = _bom[i].mara_for_sup;
                ls_Pro_Npqr.yon_unit = _bom[i].yon_unit;
                ls_Pro_Npqr.bom_name = _bom[i].bom_name;
                if (update_pdb.Exists(e => e.matnr == _bom[i].mara_for_ls))
                {
                    var u_pdb = update_pdb.Where(w => w.matnr.Trim() == _bom[i].mara_for_ls.Trim()).FirstOrDefault();
                    ls_Pro_Npqr.matnr = u_pdb.matnr;
                    ls_Pro_Npqr.pbi = u_pdb.pbi;
                    ls_Pro_Npqr.price = u_pdb.price;
                    ls_Pro_Npqr.ycpapar = u_pdb.ycpapar;
                    ls_Pro_Npqr.acknow = u_pdb.acknow;
                    ls_Pro_Npqr.btype = u_pdb.btype;
                    ls_Pro_Npqr.bzcc = u_pdb.bzcc;
                    ls_Pro_Npqr.candi = u_pdb.candi;
                    ls_Pro_Npqr.cgdd = u_pdb.cgdd;
                    ls_Pro_Npqr.dw = u_pdb.dw;
                    ls_Pro_Npqr.gycode = u_pdb.gycode;
                    ls_Pro_Npqr.gyname = u_pdb.gyname;
                    ls_Pro_Npqr.hcate = u_pdb.hcate;
                    ls_Pro_Npqr.ls_type = u_pdb.ls_type;
                    ls_Pro_Npqr.ltw = u_pdb.ltw;
                    ls_Pro_Npqr.matclass = u_pdb.matclass;
                    ls_Pro_Npqr.matx = u_pdb.matx;
                    ls_Pro_Npqr.moq = u_pdb.moq;
                    ls_Pro_Npqr.original = u_pdb.original;
                    ls_Pro_Npqr.spq = u_pdb.spq;
                    ls_Pro_Npqr.storecondition = u_pdb.storecondition;
                    ls_Pro_Npqr.syb = u_pdb.syb;

                }
                Ls_pro_npqrRepository.Instance.Add(ls_Pro_Npqr, true);
            }
                return webResponse.OK("生成成功！");
        }
    }
}
